//
//  DYRenshuChoiceView.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/3/9.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class DYRenshuChoiceView: SYBaseView  , UITableViewDelegate, UITableViewDataSource {
    
    var numsClosure: (([Int])->Void)?
    var tableView: UITableView!
    var models:[DYZSRenshuChoiceModel] = []
    var msModels:[DYZSRenshuChoiceModel] = []
    var nums:[Int] = []
    var msNums:[Int] = []
    var type: YuleType = .zhusu
    
    var bgView: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 10.autoWidth()
        v.layer.masksToBounds = true
        return v
    }()
    
    var bgBt: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = UIColor(white: 0, alpha: 0.5)
        return v
    }()
    
    var compBt: UIButton = {
        let v: UIButton = UIButton()
        v.backgroundColor = Constant.blueColor
        v.layer.cornerRadius = 10.autoWidth()
        v.layer.masksToBounds = true
        v.setTitle("完成", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 18.autoBoldFontSize()
        return v
    }()
    
    
    
    func configure(_ models: [DYZSRenshuChoiceModel]) {
        self.models = models
        models.forEach { (m) in
            nums.append(0)
        }
        
            nums[0] = 1
        
    }
    
    func configureMeishi(_ models: [DYZSRenshuChoiceModel]) {
        self.msModels = models
        msModels.forEach { (m) in
            msNums.append(0)
        }
        
            msNums[0] = 1
        
    }
    
    func show() {
        self.isHidden = false
        
        switch type {
        case .zhusu:
            
            tableView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(10.autoWidth())
                make.left.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(45*models.count.autoWidth())
            }
        case .meishi:
            
            tableView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(10.autoWidth())
                make.left.equalToSuperview()
                make.right.equalToSuperview()
                make.height.equalTo(45*msModels.count.autoWidth())
            }
        default:
            break
        }
        
        
        tableView.reloadData()
    }
    
    override func initViews() {
        self.isHidden = true
        bgBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        compBt.addAction {[weak self] in
            if let `self` = self {
                self.isHidden = true
            }
        }
        
        self.backgroundColor = .clear
        addSubview(bgBt)
        addSubview(bgView)
        addSubview(compBt)
        setTableView()
        
        bgView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(285.autoWidth() + BOTTOM_SAFE_HEIGHT)
        }
        bgBt.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        compBt.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10.autoWidth() - BOTTOM_SAFE_HEIGHT)
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(44.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(180.autoWidth())
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect.zero, style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView!.register(DYRenshuChoiceCell.self, forCellReuseIdentifier: "Cell")
        self.bgView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if type == .zhusu {
            return models.count
        } else {
            return msModels.count
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: DYRenshuChoiceCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DYRenshuChoiceCell
        cell?.selectionStyle = .none
        cell?.currentVC = self.currentVC
        
        if type == .zhusu {
        if indexPath.row == 3 {
            cell?.configureView(models[indexPath.row], nil)
        } else {
                cell?.configureView(models[indexPath.row], nums[indexPath.row])
        }
    } else {
        if indexPath.row == 3 {
            cell?.configureView(msModels[indexPath.row], nil)
        } else {
                cell?.configureView(msModels[indexPath.row], msNums[indexPath.row])
        }
    }
        
        cell?.clickedClosure = {[weak self] value in
            if let `self` = self {
                if self.type == .zhusu {
                    self.nums[indexPath.row] = value
                    self.numsClosure?(self.nums)
                } else {
                    self.msNums[indexPath.row] = value
                    self.numsClosure?(self.msNums)
                }
            }
        }
        return cell!
    }
    
}



class DYRenshuChoiceCell: SYBaseCell {
    
    var isChuangpu: Bool = false
    
    let jianPic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.layer.cornerRadius = 8.autoWidth()
        v.layer.masksToBounds = true
        v.sy_name("jian")
        return v
    }()
    
    let jiaPic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.layer.cornerRadius = 8.autoWidth()
        v.layer.masksToBounds = true
        v.sy_name("jia")
        return v
    }()
    
    var jianBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    var jiaBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    let tipLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x666666)
        v.font = 10.autoFontSize()
        
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        
        return v
    }()
    
    let countLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        v.textAlignment = .center
        return v
    }()
    
    
    public func configureView(_ model: DYZSRenshuChoiceModel, _ count: Int? = nil) {
        
        if let _ = model.synopsis {
            tipLb.snp.remakeConstraints { (make) in
                make.centerY.equalToSuperview().offset(autoWidth(8.0))
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(10.autoWidth())
            }
            nameLb.snp.remakeConstraints { (make) in
                make.centerY.equalToSuperview().offset(autoWidth(-5.0))
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(15.autoWidth())
            }
        } else {
            tipLb.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(10.autoWidth())
            }
            nameLb.snp.makeConstraints { (make) in
                make.centerY.equalToSuperview()
                make.left.equalToSuperview().offset(autoWidth(15.0))
                make.height.equalTo(15.autoWidth())
            }
        }
        nameLb.text = model.strK
        tipLb.text = model.synopsis ?? ""
        if let c = count {
            
            countLb.text = "\(c)"
            isChuangpu = false
        } else {
            countLb.text = "不限"
            isChuangpu = true
        }
    }
    
    override func initViews() {
        
        jianBt.addTarget(self, action: #selector(jianAction), for: .touchUpInside)
        jiaBt.addTarget(self, action: #selector(jiaAction), for: .touchUpInside)
        
        
        contentView.addSubview(jianPic)
        contentView.addSubview(jiaPic)
        contentView.addSubview(jianBt)
        contentView.addSubview(jiaBt)
        contentView.addSubview(tipLb)
        contentView.addSubview(countLb)
        contentView.addSubview(nameLb)
        contentView.addSubview(lineView)
        
        jianPic.snp.makeConstraints { (make) in
            
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-69.0))
            make.height.equalTo(16.autoWidth())
            make.width.equalTo(16.autoWidth())
        }
        jiaPic.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.autoWidth())
            make.width.equalTo(16.autoWidth())
        }
        jianBt.snp.makeConstraints { (make) in
            
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-62.0))
            make.height.equalTo(30.autoWidth())
            make.width.equalTo(30.autoWidth())
        }
        jiaBt.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-8.0))
            make.height.equalTo(30.autoWidth())
            make.width.equalTo(30.autoWidth())
        }
        
        countLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(autoWidth(-31.0))
            make.height.equalTo(16.autoWidth())
            make.width.equalTo(38.autoWidth())
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(0.5.autoWidth())
        }
        tipLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.height.equalTo(10.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.height.equalTo(15.autoWidth())
        }
    }
    
    @objc func jianAction() {
        
        if isChuangpu {
            if let n = Int(countLb.text!) {
                if n == 1 {
                    self.clickedClosure?(n)
                    countLb.text = "不限"
                } else if n > 1 {
                    self.clickedClosure?(n - 1)
                    countLb.text = "\(n - 1)"
                }
            } else {
                
            }
            
        } else {
            let num = Int(countLb.text!)! - 1
            if num >= 0 {
                self.clickedClosure?(num)
                countLb.text = "\(num)"
            }
        }
        
    }
    
    @objc func jiaAction() {
        
        var num = 0
        if let n = Int(countLb.text!) {
            num = n + 1
        } else {
            num = 1
        }
        
        countLb.text = "\(num)"
        self.clickedClosure?(num)
    }
}
